package doInterce.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import doInterce.DoInterface;
import org.apache.commons.lang.StringUtils;
import utils.MapEleInfo;
import utils.UnitInfo;
import utils.YmlUtil;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description: 解析统计库的机构树
 * @Author: syh
 * @Date: 2023/12/18/11:32
 */
public class DoTree implements DoInterface {
    public void doExecute() {

        Map<String,Object> map = YmlUtil.getYmlMap();
        String outPath = Convert.toStr(map.get("outPath"));

        List<UnitInfo> allUnitList = new ArrayList<>();
        Map<String, MapEleInfo> b0111Map = readFile(new HashMap<>());
        setOrderNum(b0111Map);
        List<String> contextList = new ArrayList<>();
        for(String key : b0111Map.keySet()){
            List<UnitInfo> unitInfoList = b0111Map.get(key).getUnitInfoList();
            for (int i = 0; i < unitInfoList.size(); i++) {
                UnitInfo unitInfo = unitInfoList.get(i);
                unitInfo.setOrderNum(i+1);

            }

            allUnitList.addAll(unitInfoList);
        }

        Collections.sort(allUnitList, Comparator.comparing(UnitInfo::getB0111).thenComparing(UnitInfo::getOrderNum));
        for (int i = 0; i < allUnitList.size(); i++) {
            UnitInfo unitInfo = allUnitList.get(i);
            String str = unitInfo.getB0111()+" "+unitInfo.getB0121()+" "+unitInfo.getB0101()+" "+unitInfo.getType()+" "+unitInfo.getOrderNum()+" ";
            if(StringUtils.isNotBlank(unitInfo.getUnlb())){
                str += unitInfo.getUnlb();
            }
            contextList.add(str);
        }

//        Collections.sort(allUnitList, (o1, o2) -> {
//            String o1sj = o1.getB0111()==null?"":o1.getB0111();//任职时间
//            String o2sj = o2.getOrderNum()==null?"":o2.getOrderNum();//任职时间
//            if("".equals(o1sj)){
//                return -1;
//            }else if("".equals(o2sj)){
//                return 1;
//            }else{
//                if(o1sj.length()>=6){
//                    String d1 = o1sj.substring(0,6);
//                    String d2 = o2sj.substring(0,6);
//                    return d1.compareTo(d2);
//                }
//            }
//            return 0;
//        });


        FileUtil.writeLines(contextList, outPath, "UTF-8");
    }

    public static void setOrderNum(Map<String, MapEleInfo> b0111Map){
        b0111Map.keySet().forEach(key -> {
            List<UnitInfo> unitInfoList = b0111Map.get(key).getUnitInfoList();
            for (int i = 0; i < unitInfoList.size(); i++) {
                UnitInfo unitInfo = unitInfoList.get(i);
                unitInfo.setOrderNum(i+1);
            }
        });
    }
    public static Map<String, MapEleInfo> readFile(Map<String,String> unlbMap){
        StringBuffer sb = new StringBuffer();
        Map<String, MapEleInfo> b0111Map = new HashMap<>();
        Map<String,Object> map = YmlUtil.getYmlMap();
        String txtPath = Convert.toStr(map.get("txtPath"));
        String logPath = Convert.toStr(map.get("logPath"));
        String startB0111 = Convert.toStr(map.get("startB0111")).replaceAll("A", "");
        List<String> context = FileUtil.readLines(txtPath, "UTF-8");

        for (int i = 0; i < context.size(); i++) {
            String line = context.get(i);
            line = line.replaceAll(" ","");
            System.out.println(line);

            String num = null;
            String name = null;
            Pattern pattern = Pattern.compile("(\\d+)");
            Matcher matcher = pattern.matcher(line);
            if (matcher.find()) {
                num = matcher.group();
//                name = line.replace(num, "");
                name = line;
            }else{
                sb.append(line+"====没有找到\n");
                break;
            }

            if("1".equalsIgnoreCase(num)){
                b0111Map.put("",new MapEleInfo(startB0111,new ArrayList<>()));
            }else{
                String parentNum = num.substring(0,num.length()-2);
                MapEleInfo mapEleInfo = b0111Map.get(parentNum);
                List<UnitInfo> unitInfos = mapEleInfo.getUnitInfoList();

                String b0111;
                if(!CollUtil.isEmpty(unitInfos)){
                    UnitInfo maxInfo = unitInfos.get(unitInfos.size() - 1);
                    String maxB0111 = maxInfo.getB0111().substring(maxInfo.getB0111().length()-3);
                    String danIdTemp = Integer.toString(Integer.valueOf(maxB0111, 36)+1, 36);
                    b0111 = mapEleInfo.getB0111()+"."+"00000000".substring(0, 3-danIdTemp.length())+danIdTemp;
                }else{
                    b0111 = mapEleInfo.getB0111()+".001";
                }

                if(unitInfos != null){
                    unitInfos.add(new UnitInfo(b0111,mapEleInfo.getB0111(),name,null,unlbMap.get(name)));
                }

                b0111Map.put(num,new MapEleInfo(b0111,new ArrayList<>()));
            }

        }

        FileUtil.writeUtf8String(sb.toString(),logPath);
        return b0111Map;
    }
}
